package com.ruoyi.assets.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.assets.service.IAssetsinformationService;
import com.ruoyi.base.domain.Warehouse;
import com.ruoyi.base.service.IWarehouseService;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.assets.domain.Storagereceipt;
import com.ruoyi.assets.service.IStoragereceiptService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 入库管理Controller
 * 
 * @author ruoyi
 * @date 2023-05-04
 */
@RestController
@RequestMapping("/assets/storagereceipt")
public class StoragereceiptController extends BaseController
{
    @Autowired
    private IStoragereceiptService storagereceiptService;
    @Autowired
    private IWarehouseService warehouseService;
    @Autowired
    private IAssetsinformationService assetsinformationService;

    /**
     * 查询入库管理列表
     */
    @PreAuthorize("@ss.hasPermi('assets:storagereceipt:list')")
    @GetMapping("/list")
    public TableDataInfo list(Storagereceipt storagereceipt)
    {
        startPage();
        storagereceipt.setType(1);
        List<Storagereceipt> list = storagereceiptService.selectStoragereceiptList(storagereceipt);
        return getDataTable(list);
    }

    /**
     * 导出入库管理列表
     */
    @PreAuthorize("@ss.hasPermi('assets:storagereceipt:export')")
    @Log(title = "入库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Storagereceipt storagereceipt)
    {
        storagereceipt.setType(1);
        List<Storagereceipt> list = storagereceiptService.selectStoragereceiptList(storagereceipt);
        ExcelUtil<Storagereceipt> util = new ExcelUtil<Storagereceipt>(Storagereceipt.class);
        util.exportExcel(response, list, "入库管理数据");
    }

    /**
     * 获取入库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('assets:storagereceipt:query')")
    @GetMapping(value = {"/","/{storageReceiptid}"})
    public AjaxResult getInfo(@PathVariable(value = "storageReceiptid",required = false) Long storageReceiptid)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("warehouseList", warehouseService.selectWarehouseAll());
        ajax.put("assetsList",assetsinformationService.selectAssetsinformationAll());
        System.out.println(assetsinformationService.selectAssetsinformationAll());
        if (StringUtils.isNotNull(storageReceiptid))
        {
            ajax.put(AjaxResult.DATA_TAG, storagereceiptService.selectStoragereceiptByStorageReceiptid(storageReceiptid));
        }
        return ajax;
    }

    /**
     * 新增入库管理
     */
    @PreAuthorize("@ss.hasPermi('assets:storagereceipt:add')")
    @Log(title = "入库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Storagereceipt storagereceipt)
    {
        storagereceipt.setCreateUserid(getUserId());
        storagereceipt.setCreateUsername(getUsername());
        storagereceipt.setType(1);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String number = sdf.format(System.currentTimeMillis());//202211041552160219
        System.out.println("根据当前时间戳生成唯一："+number);

        String serialNumber = "R"+number;
        storagereceipt.setTicketNo(serialNumber);
        return toAjax(storagereceiptService.insertStoragereceipt(storagereceipt));
    }

    /**
     * 修改入库管理
     */
    @PreAuthorize("@ss.hasPermi('assets:storagereceipt:edit')")
    @Log(title = "入库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Storagereceipt storagereceipt)
    {
        storagereceipt.setEditUsername(getUsername());
        storagereceipt.setEditUserid(getUserId());
        return toAjax(storagereceiptService.updateStoragereceipt(storagereceipt));
    }

    /**
     * 删除入库管理
     */
    @PreAuthorize("@ss.hasPermi('assets:storagereceipt:remove')")
    @Log(title = "入库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{storageReceiptids}")
    public AjaxResult remove(@PathVariable Long[] storageReceiptids)
    {
        return toAjax(storagereceiptService.deleteStoragereceiptByStorageReceiptids(storageReceiptids));
    }


    /**
     * 获取仓库列表
     */
    @PreAuthorize("ss.hasPermi('assets:storagereceipt:query')")
    @GetMapping("/warehouseList")
    public AjaxResult warehouseList() {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("warehouseList", warehouseService.selectWarehouseAll());
        System.out.println(warehouseService.selectWarehouseAll());
        return ajax;
    }
}
